<?php


/**
 * 或许URL
 * @return string
 */
function getCurUrl() {
    $url = 'http://';
    if(isset($_SERVER['SERVER_HTTPS']) && $_SERVER['SERVER_HTTPS'] == 'on') {
        $url = 'https://';
    }
    //判断端口
    if($_SERVER['SERVER_PORT'] != '80') {
        $url .= $_SERVER['SERVER_NAME'] .':'.$_SERVER['SERVER_PORT'];
    }else{
        $url .= $_SERVER['SERVER_NAME'];
    }
    return $url;
}


/**
 * 获取用户IP
 * @return array|false|string
 */
function getClientIP(){
    if (getenv("HTTP_CLIENT_IP")) {
        $ip = getenv("HTTP_CLIENT_IP");
    }elseif(getenv("HTTP_X_FORWARDED_FOR")) {
        $ip = getenv("HTTP_X_FORWARDED_FOR");
    }elseif(getenv("REMOTE_ADDR")) {
        $ip = getenv("REMOTE_ADDR");
    }else{
        $ip = "Unknow";
    }
    return $ip;
}


/**
 * 是否是GET请求
 * @return bool
 */
function isGet(){
    return $_SERVER['REQUEST_METHOD'] == 'GET' ? true : false;
}

/**
 * 是否是POST请求
 * @return bool
 */
function isPost(){
    return $_SERVER['REQUEST_METHOD'] == 'POST' ? true : false;
}

/**
 * 过滤参数
 * @param string $str 接受的参数
 * @return string
 */
function filterWords($str)
{
    $farr = array(
        "/<(\\/?)(script|i?frame|style|html|body|title|link|meta|object|\\?|\\%)([^>]*?)>/isU",
        "/(<[^>]*)on[a-zA-Z]+\s*=([^>]*>)/isU",
        "/select|insert|update|delete|\'|\/\*|\*|\.\.\/|\.\/|union|into|load_file|outfile|dump/is"
    );
    $str = preg_replace($farr,'',$str);
    return $str;
}

/**
 * 取GET数据
 * @return array
 */
function getData(){
    $arr = $_GET;
    if(is_array($arr)){
        foreach($arr as $k => $v){
            $arr[$k] = filterWords($v);
        }
    }
    return $arr;
}

/**
 * 取POST数据
 * @return array
 */
function postData(){
    $arr = $_POST;
    if(is_array($arr)){
        foreach($arr as $k => $v){
            $arr[$k] = filterWords($v);
        }
    }
    return $arr;
}